// import {getUserInfo} from '../../utils/apiData'
import {
    wechat
} from '../../api/apiData'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        type: '',
        token: '',
        tokenS: '',
        groupId: '',
        buttonClicked: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        wx.login({
            success(res) {
                that.setData({
                    code: res.code
                })
            }
        })
    },

    /**
     * 授权登录
     */
    getUserInfos(e) {
        let _this = this
        wx.showLoading({
            title: "请稍等...",
        })
        let iv, encryptedData
        wx.getUserProfile({
            desc: "个人信息",
            success: function (res) {
                iv = res.iv
                encryptedData = res.encryptedData
                wx.login({
                    success(res) {
                        wechat.login({
                            code: res.code,
                            iv,
                            encryptedData
                        }, res => {
                            let {
                                code,
                                msg,
                                data
                            } = res
                            console.log(res)
                            if (code == 0) {
                                wx.showToast({
                                    title: res.msg,
                                    icon: 'error'
                                })
                            } else {
                                wx.showToast({
                                    title: res.msg,
                                })
                                wx.setStorage({
                                    key: "token",
                                    data: data.token
                                })
                                wx.setStorage({
                                    key: "type",
                                    data: 'User'
                                })
                                setTimeout(()=>{
                                    wx.navigateBack({
                                        delta: 0,
                                    })
                                },500)
                               
                            }

                        })
                    },
                    fail(err) {
                        wx.hideLoading();
                    }
                })
            },
            fail(err) {
                wx.hideLoading();
            }
        })

    },

    /**
     * 暂不登录
     */
    onNotLogin() {
        let _this = this;
        // 跳转回原页面
        _this.onNavigateBack();
    },

    /**
     * 授权成功 跳转回原页面
     */
    onNavigateBack() {
        wx.navigateBack();
    },

})